//
//  LastedMessagesListViewController.m
//  NewApp
//
//  Created by  on 12-6-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LastedMessagesListViewController.h"
#import "NAMessage.h"
#import "Constants.h"
#import "NAStyledTableViewCell.h"

@interface LastedMessagesListViewController ()

/**
 *  Load lasted messages by page
 */
- (void) loadLastedMessagesByPageNum:(NSInteger) pageNum;

/**
 *  lasted messages did load
 */
- (void) lastedMessagesDidLoadFinished:(RequestEngine *) engine withObject:(id) object;

/**
 *  Returns message at index
 */
- (NAMessage *) messageAtIndex:(NSInteger) index;

/**
 *  Update UI by style
 */
- (void) updateUIByStyle:(NSString *) type dataObjects:(NSArray *) dataObjs;
 
@end

@implementation LastedMessagesListViewController
@synthesize tableView = tableView_,datasourceObjects = datasourceObjects_,galleryView = galleryView_;

#pragma mark -
#pragma mark - Views lifecycle and memory management

- (void)viewDidLoad {
    [super viewDidLoad];
    
    currentPageNum = 1;
    
    //Load lated message
    [self loadLastedMessagesByPageNum:1];
}

- (void) dealloc {
    [tableView_ release];
    [datasourceObjects_ release];
    [galleryView_ release];
    [super dealloc];
}


#pragma mark -
#pragma mark - Read only property accessories

- (UITableView *) tableView {
    if (nil == tableView_) {
        tableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320.f, 367.f) 
                                                        style:UITableViewStylePlain];
        tableView_.dataSource = self;
        tableView_.delegate = self;
        [self.view addSubview:tableView_];
    }
    
    return tableView_;
}

- (NSMutableArray *) datasourceObjects {
    if (nil == datasourceObjects_) {
        datasourceObjects_ = [[NSMutableArray alloc] init];
    }
    
    return datasourceObjects_;
}

- (ThumbnailGalleryView *) galleryView {
    if (nil == galleryView_) {
        galleryView_ = [[ThumbnailGalleryView alloc] initWithFrame:CGRectMake(0, 26, 320, 480)];
    }
    
    return galleryView_;
}


#pragma mark -
#pragma mark - UITableView delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark -
#pragma mark - UITableView datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasourceObjects.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cellidentifier";
    NAStyledTableViewCell *cell = (NAStyledTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NAStyledTableViewCell alloc] initWithNAStyle:@"D" reuseIdentifier:cellIdentifier];
    }
    
    [cell updateCellWithEntity:[self.datasourceObjects objectAtIndex:indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView *v = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    v.backgroundColor = (indexPath.row % 2 == 0) ? [UIColor colorWithRed:81.f/255.f green:81.f/255.f blue:81.f/255.f alpha:1.f] : [UIColor colorWithRed:51.f/255.f green:51.f/255.f blue:51.f/255.f alpha:1.f];
    cell.backgroundView = v;
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85.f;
}

#pragma mark -
#pragma mark - Private methods

- (void) loadLastedMessagesByPageNum:(NSInteger)pageNum {
    if (messageReqEngine != nil) 
        return;
    
    messageReqEngine = [[RequestEngine alloc] initWithTarget:self action:@selector(lastedMessagesDidLoadFinished:withObject:)];
    [messageReqEngine lastedMessagesListByPage:pageNum];
}

- (void) lastedMessagesDidLoadFinished:(RequestEngine *)engine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        if ([dict objectForKey:kData] != nil) {
            NSArray *array = [dict objectForKey:kData];
            for (NSDictionary *dic in array) {
                NAMessage *message = [[NAMessage alloc] initWithDictionary:dic];
                [self.datasourceObjects addObject:message];
            }
            
            [self.tableView reloadData];
            //[self updateUIByStyle:[dict objectForKey:@"style"] dataObjects:array];
        }
    
        [dict release];
    }
    else {
        [[NAError errorWithCode:NAError_REQ_FAIL] showErrorMessage:@""];
    }
}

- (NAMessage *) messageAtIndex:(NSInteger) index {
    if (index <0 || index >= self.datasourceObjects.count) {
        return nil;
    }
    
    return [self.datasourceObjects objectAtIndex:index];
}

- (void) updateUIByStyle:(NSString *) type dataObjects:(NSArray *) dataObjs {
    if (type == nil || type.length == 0) {
        currentStyle = kStyleA; 
    }
    
    if ([currentStyle isEqualToString:kStyleA]) {
        NSMutableArray *thumbnailImages = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dic in dataObjs) {
            NAImage *product = [[NAImage alloc] initWithDictionary:dic];
            [thumbnailImages addObject:product];
            [product release];
        }
        
        [self.galleryView setGalleryWithLargeImages:thumbnailImages];
        [self.view addSubview:self.galleryView];
        
        [thumbnailImages release];

    }
}

@end
